import requests
from lxml import etree
import re
import time
import random
import sys
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf8')
with open('a.txt', "a+") as ff:
    row = 0
    for p in range(1, 23):
        time.sleep(random.randint(1, 100)/10)
        u = "https://www.ruyile.com/xuexiao/?a=305&t=3&p=%s" %  p
        try:
            html = requests.get(u)
        except:
            print("等待1秒重试")
            time.sleep(1)
            html = requests.get(u)

        selector = etree.HTML(html.text)
        ti = selector.xpath(
            "/html/body/div[2]/div[6]/div[1]/div[2]/div[1]/text()")
        ts = selector.xpath(
            '/html/body/div[2]/div[6]/div[1]/div[2]/div[@class="sk"]')
        for x in ts:
            t1 = x.xpath('./h4/a/text()')
            t2 = x.xpath('text()')
            dd = ti+t1+t2
            print(dd,row)
            row = row+1            
            ff.write("#".join(dd)+'\n')
